<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<link rel="stylesheet" href="../../css/mui.min.css">
	<script src="../../js/app.js"></script>
	<script src="../../js/mui.min.js"></script>
	<script src="../../js/upload.js"></script>
	<title>发布</title>
	<style type="text/css">  
select{
	padding: 3px;
	margin: 2px;
	font-size: 17px;
}
textarea{
	width:98%;margin-left:1%;margin-top: 1px;font-size: 17px;
}
</style> 
</head>
<body>
	<header class="mui-bar mui-bar-nav">
	    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
	    <h1 class="mui-title">发布</h1>
	</header>

<ul id="opta" class="mui-table-view"style="width: 100%;height:100%;position:absolute;font-size:2em;text-align:center;z-index: 100;display:none;">
    <li class="mui-table-view-cell"></li>

</ul>
	<!--<div class="mui-content">-->
<div id="uploader" class="mui-content"style="font-size: 17px;">
   <div id="picture" name="picture" style="margin-left:1px;"><img src="" alt="" /></div> 
   <div id="myBtn" style="width:100px;margin:6px;float:left;height:100px;padding:10px;" ><img  style="float:initial;width:80px;" src="../../images/iconfont-tianjia.png"/></div>
   <textarea maxlength="300" type="text" name="mess" id="mess" rows="3" cols="30" placeholder="请输入文字内容200字以内"></textarea>
    <div class="mui-input-row">
        <!--<label>关联</label>-->
    <input type="text" onclick="openlist('opta')" style="text-align:center;" id="relation" placeholder="添加关联-来自收藏夹(可选)">
    </div>
    <button type="button" data-loading-text="提交中" onclick="ok()"  style="float:right;margin-right: 1%;width: 98%;margin-top: 8px;" class="mui-btn mui-btn-primary">发送</button>
</div> 
<!--</div>--> 
<script type="text/javascript" >
opta=document.getElementById('opta');
function openlist(htmlid){
	Show_Hidden(htmlid);
	selectone(); 
}
var opta=document.getElementById("opta");//定义下拉列表主表
var listJson={'0':'选择关联类','item':'项目','resource':'资源','resume':'技术'};//定义主表菜单列表
//selectone();//执行下拉选择列表
function selectone(){
var i=0;
var lisk='';
for(var p in listJson){//遍历json对象的每个key/value对,p为key
	lisk+='<li class="mui-table-view-cell" value="'+p+'" onclick="listone(\''+p+'\',\''+listJson[p]+'\')">'+listJson[p]+'</li>';
}
opta.innerHTML=lisk+'<li type="button"  class="mui-table-view-cell" value="" onclick="Show_Hidden(\'opta\');lookrelation(\'\',\'\',\'\')">取消</li>';

}
function Show_Hidden(trid){
	var html=document.getElementById(trid);
    if(html.style.display=="block"){
        html.style.display='none';
    }else{
        html.style.display='block';
    }
}
function listone(mod,str){
//	alert(mod+str);
	apptoken['mod']=mod||'';
	apptoken['act']='list' ||'';
	mui.post(home.hos+'/Collect/user',apptoken,function(data){
//		alert(data);
		if(isJSON(data)){
			result= JSON.parse(data);
			if(result.length===0){
				alert('对不起不存在'+listJson[opta.value]+'信息,请您发布该类信息后，再添加关联！');
//				selectone();//还原下拉列表
			return false;
			};
			//key是本地数据库的键值，
			var vals='';
			for(var i=0;i<result.length;i++){
		//	alert(result[i].id+'":"'+result[i].title);
			vals +='<li class="mui-table-view-cell" onclick="Show_Hidden(\'opta\');lookrelation(\''+mod+'\',\''+result[i].tid+'\',\''+result[i].title+'\')">'+result[i].title+'</li>';
			}
		opta.innerHTML=vals+'<li type="button"  class="mui-table-view-cell" value="" onclick="Show_Hidden(\'opta\');lookrelation(\'\',\'\',\'\')">取消</li>';
		return data;
		}else{
			document.getElementById("relation").value='您未发布'+str+'主题';
		}
})}
function lookrelation(mod,rid,title){
	document.getElementById("relation").value=title;
	apptoken['relation']=mod;
	apptoken['relation_id']=rid;
	apptoken['title']=title;
if(title=='' || rid ==''){
	delete apptoken['relation'];
	delete apptoken['relation_id']; 
	delete apptoken['title'];
	delete apptoken['mod'];	
}
}

//alert(localStorage.itemid);
	window.addEventListener('renovate', function(e){
		//裁剪后返回的图片显示在image标签下的img
		document.getElementById('image').src=e.detail.image;
				//主图裁剪上传返回数据执行更新
//			  location.reload(); //刷新
			  });
	function goimage() {
		tid=localStorage.itemtid;
		act='resume';
		method='resume';
		width=300;
		height=300;
		localStorage.image='{"tid":"'+tid+'","act":"up'+act+'","method":"'+method+'","src":"","width":'+width+',"height":'+height+'}';
		mui.openWindow('../../Plugin/image/base64.html');
	};

var type=64;//可不填。返回类型（file/64base）;
var imgnames='imgs';//img对应的name值
var upload=new uploadimg(imgsplay,type);//实例化类
var picture=document.getElementById('picture');
function imgsplay(path){//两个参数path必填是图片路径，type=64可填，返回数据类型。默认不填返回图片源文件,64=返回64base压缩类型
if(type===64){//判断是否需要64base转码压缩
		var image = new Image();
			image.src = path;  
			image.onload = function(){
			var img_typearr=['jpeg','jpg','bmp','png'];//支持格式类型
			_type=upload.appendFile(img_typearr,path);//检查是否支持,返回false 或者后缀名
			if(_type){
			var is_type="image/jpeg";//压缩格式
			var ratio=0.9;//压缩质量
			var width=1080;//压缩后最长边距像素；
		    path = compress(image,width, ratio,is_type);//压缩图片转成64base后显示
		    imgplay(path);
			}}
}else{
	imgplay(path);
}
function imgplay(path){//
	picture.innerHTML+='<div  id="'+path+'" onclick="upload.delete_id(this.id,picture)" style="width:30%;float:left;height:100px;margin:1%;overflow:hidden;position:relative;font-size:10px;border-radius:5px;" ><span style="background:#FF6A6A;margin-top:0;border-radius:6px;width:18px;height:18px;position:absolute;text-align :center;color:#fff;line-height:18px; z-index:1;">X</span><img name="imgs" style=" position:relative;  z-index:0;max-height: 200%;max-width: 200%;width: auto;height: auto;top: 50%;left: 50%;-webkit-transform: translateX(-50%) translateY(-50%);transform: translateX(-50%) translateY(-50%);" name="'+imgnames+'" src="'+path+'" alt="" /></div>';
}
};

mui.plusReady(function() {//初始化 
	var maxnum=9;//限制上传图片数量
document.getElementById("myBtn").onclick=function(){//单击id
	var lasnum=maxnum-document.getElementsByName(imgnames).length;
	upload.addImage(maxnum,lasnum,'picture');//图片img外层id
};
})

function ok(){
//	alert(JSON.stringify(apptoken));
	var sizenum=1;
	if(document.getElementById('mess').value.length<sizenum){
		alert('故事简介最少需要'+sizenum+'字，请从新提交');
		return false;
	}
	// 得到页面中所有input	
	var allimg = document.getElementsByName('imgs');
	var allimgarr = [];
	// 在所有Input中过滤出type="text" and name="libID"的元素，放到数组libIds中  && allInputs[i].name === 'libname'
	for (var i = 0; i < allimg.length; i++) {
		if (allimg[i].src != '') 
		{
//			alert(allimg[i].src);
			allimgarr.push(allimg[i].src);//把图片装入数组
			}
		}
	apptoken['act']='publish';
mui.post(home.hos+'/news/admin',Object.assign(apptoken,{
		content:document.getElementById('mess').value,

		imgarr:allimgarr
	}),function(data){
//		alert('服务器返回'+data);
		if(data!=200){
			plus.ui.toast("发布失败,请重新尝试，错误码".data);
		}else{
				plus.ui.toast("发布成功");
				mui.back();
				var list = plus.webview.currentWebview().opener();
				//触发父页面的自定义事件(refresh),从而进行刷新
				 mui.fire(list, 'refresh');
				//返回true,继续页面关闭逻辑
				return true;
				}
		//服务器返回响应，根据响应结果，分析是否登录成功；
	},'text'
);}
</script>
</body>
</html>